home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-04-26 | 1.5 KB | 93 lines | [TEXT/CWIE] |
- /*
- ©1996 Peter N Lewis <peter@stairways.com.au>
- */
-
- package au.com.peter.libraries;
-
- import java.io.*;
- import java.net.*;
- import java.lang.*;
-
- public class TwoPlayerClient {
-
- public static final int DEFAULT_PORT = 1465;
-
- private Socket conn = null;
- private DataInputStream sin = null;
- private DataOutputStream sout = null;
-
-
- public TwoPlayerClient( String host ) throws Exception {
- this( host, DEFAULT_PORT );
- }
-
-
- public TwoPlayerClient( String host, int port ) throws Exception {
- if ( port == 0 ) {
- port = DEFAULT_PORT;
- }
-
- try {
- conn = new Socket(host, port);
- sin = new DataInputStream(conn.getInputStream());
- sout = new DataOutputStream(new BufferedOutputStream(conn.getOutputStream()));
- } catch (Exception e) {
- close();
- throw e;
- }
-
-
- }
-
-
- protected void finalize() {
- close();
- }
-
-
- public void close() {
- sin = null;
- sout = null;
- if ( conn != null ) {
- try {
- conn.close();
- } catch (IOException e2) {
- }
- }
- conn = null;
- }
-
-
- public InetAddress getInetAddress() {
- return conn.getInetAddress();
- }
-
-
- public int getPort() {
- return conn.getPort();
- }
-
-
- public void WriteLine( String line ) throws IOException {
- sout.writeShort( line.length() );
- sout.writeBytes( line );
- sout.flush();
- }
-
-
- public String ReadLine() {
- String line;
- try {
- int length = sin.readUnsignedShort();
- byte b[] = new byte[length];
- sin.read( b );
- line = new String( b, 0 );
- } catch (IOException e) {
- close();
- line = null;
- }
- return line;
- }
-
- }
-